<template>
    <view class="body">
        <view class="top_info">
            <image v-if="isBegin" class="color_img" src="https://ainengli.hzjrsw.com/jkhx/answerBg.png"></image>
            <view class="title">{{ questionInfo.scaleName }}</view>
            <view class="label">
                <image class="label_img" src="@/static/image/raozg/shield.png" />
                <text>认知障碍自评表AD8</text>
            </view>
            <image v-if="!isBegin" class="bg_img" src="@/static/image/raozg/questionBg.png" />
        </view>
        <view v-if="!isBegin" class="remark">
            <view class="remark_title">问卷说明:</view>
            <text class="remark_text">{{ questionInfo.remark }}</text>
        </view>
        <view v-if="isBegin" class="question_body">
            <view class="question_num">
                <text>第{{ now + 1 }}题</text>
                <text>/共{{ questionList.length }}题</text>
            </view>
            <view class="remind">请根据实际情况，选择最符合的一项</view>
            <view class="topic_title">{{ questionList[now].questionName }}</view>
            <view class="topics">
                <view
                    v-for="(item, index) of questionList[now].options"
                    :key="index"
                    :class="['topic', questionList[now].answer === index ? 'active' : '']"
                    @tap="answerSelect(item, index)"
                >{{ item.value }}{{ item.answer }}</view>
            </view>
        </view>
        <view v-if="isBegin" class="bottom">
            <view v-if="now != 0" @tap="prev" class="prev">上一题</view>
            <view 
                v-if="now + 1 == questionList.length && isFinish" 
                @tap="finish" 
                class="finish">完成自测</view>
        </view>
        <view v-if="!isBegin" class="fixed">
            <button class="confirm_btn" @tap="begin">开始自测</button>
        </view>
    </view>
</template>

<script setup lang="ts">
import { onLoad, onShow } from '@dcloudio/uni-app'
import { scale_list, scale_save } from '@/api/body'
import { ref, reactive } from 'vue'
import { debounce, throttle, deepCopy } from '@/utils/tools'

const userInfo = ref({})
const questionInfo = ref({})
const questionList = ref([])
const isBegin = ref(false)
const isFinish = ref(false)
const now = ref(0)

let currentId = ''


onShow(() => {
    userInfo.value = JSON.parse(uni.getStorageSync('userInfo')).patientInfo
    if (!currentId || currentId !== userInfo.value.empi) {
        currentId = deepCopy(userInfo.value.empi)
        getData()
    }
})

const getData = () => {
  // currentId
  scale_list(currentId).then((res) => {
    if (res.code === 0) {
        questionInfo.value = res.data[0]
        let arr = JSON.parse(res.data[0].questions)
        arr.forEach(item => {
            item.answer = ''
            item.options = JSON.parse(item.options)
        })
        questionList.value = arr
    }
  })
}

const begin = () => {
    isBegin.value = true
}

const answerSelect = throttle((item: any, index: number) => {
    questionList.value[now.value].answer = index
    questionList.value[now.value].sort = item.sort
    questionList.value[now.value].chosen = item.value
    setTimeout(() => {
        if (now.value < questionList.value.length - 1) {
            now.value += 1
        } else {
            isFinish.value = true
        }
    }, 500)
}, 500)
const prev = () => {
    now.value -= 1
}
const finish = () => {
    let num = 0
    questionList.value.forEach(item => {
        if (item.answer === 0) {
            num++
        }
    })
    let params = {
        id: questionInfo.value.id,
        empi: currentId,
        integrity: questionInfo.value.integrity,
        scaleCode: questionInfo.value.scaleCode,
        riskElements: questionInfo.value.riskElements,
        questions: "",
        answers: JSON.stringify(questionList.value),
        isFinish: true,
        score: num
    }
    scale_save(params).then(res => {
        if (res.code == 0) {
            uni.redirectTo({ 
                url: '/pages/body/result?score=' + num + '&title=' + questionInfo.value.scaleName
            })
        }
    })
}
</script>

<style lang="scss" scoped>
.body {
    width: 100%;
    height: 100vh;
    overflow: auto;
    padding-bottom: 184rpx;
    box-sizing: border-box;
    position: relative;
    .top_info {
        height: 512rpx;
        background: linear-gradient( 180deg, rgba(172,235,242,0.28) 0%, #F3F7F8 100%);
        padding: 48rpx 32rpx;
        position: relative;
        .color_img {
            position: absolute;
            left: 0;
            top: 0;
            z-index: 1;
            width: 100%;
            height: 328rpx;
        }
        .title {
            font-weight: bold;
            font-size: 48rpx;
            color: #222222;
            margin-bottom: 16rpx;
            position: relative;
            z-index: 2;
        }
        .label {
            display: inline-block;
            background: #00D1B6;
            border-radius: 28rpx;
            border: 1rpx solid #00D1B6;
            font-weight: 400;
            font-size: 26rpx;
            color: #FFFFFF;
            padding: 10rpx 32rpx;
            position: relative;
            z-index: 2;
            .label_img {
                width: 28rpx;
                height: 28rpx;
                margin-right: 8rpx;
                transform: translateY(4rpx);
            }
        }
        .bg_img {
            width: 320rpx;
            height: 334rpx;
            position: absolute;
            right: 0;
            top: 0;
            z-index: 1;
        }
    }
    .remark {
        background: #FFFFFF;
        border-radius: 16rpx;
        border: 1rpx solid #FFFFFF;
        padding: 32rpx;
        margin: 0 32rpx;
        margin-top: -235rpx;
        position: relative;
        z-index: 2;
        .remark_title {
            font-weight: 600;
            font-size: 36rpx;
            color: #222222;
            margin-bottom: 32rpx;
        }
        .remark_text {
            display: inline-block;
            font-weight: 400;
            font-size: 32rpx;
            color: #666666;
            line-height: 64rpx;
            text-align: justify;
        }
    }
    .question_body {
        background: #fff;
        border-radius: 16rpx;
        padding: 32rpx;
        margin: 0 32rpx;
        margin-top: -256rpx;
        font-size: 28rpx;
        position: relative;
        z-index: 2;
        .question_num {
            font-weight: 600;
            color: #666666;
        }
        .remind {
            font-weight: 400;
            color: #999999;
            margin-top: 16rpx;
        }
        .topic_title {
            font-size: 32rpx;
            font-weight: 400;
            color: #222222;
            margin-top: 40rpx;
        }
        .topics {
            margin-top: 40rpx;
            .topic {
                width: 630rpx;
                height: 88rpx;
                line-height: 88rpx;
                background: #fafafa;
                font-weight: 500;
                font-size: 30rpx;
                padding-left: 32rpx;
                border-radius: 8rpx;
                text-align: left;
                margin-bottom: 24rpx;
            }
            .active {
                background: rgba(0,209,182,0.05);
                border: 1rpx solid #00d1b6;
                color: #00d1b6;
            }
        }
    }
    .bottom {
        margin: 32rpx;
        display: flex;
        justify-content: space-between;
        font-weight: 500;
        font-size: 32rpx;
        gap: 26rpx;
        .prev, .finish {
            width: 50%;
            height: 88rpx;
            line-height: 88rpx;
            text-align: center;
            border-radius: 8rpx;
        }
        .prev {
            border: 1rpx solid #00d1b6;
            color: #00D1B6;
        }
        .finish {
            color: #fff;
            background: #00d1b6;
        }
    }
    .fixed {
        position: fixed;
        width: 100%;
        bottom: 0;
        left: 0;
        z-index: 10;
        padding: 24rpx 32rpx;
        padding-bottom: calc(24rpx + constant(safe-area-inset-bottom));
        padding-bottom: calc(24rpx + env(safe-area-inset-bottom));
        box-sizing: border-box;
        background: #ffffff;
        .confirm_btn {
            width: 100%;
            background: #00d1b6;
            border-radius: 44rpx;
            font-weight: bold;
            font-size: 32rpx;
            color: #ffffff;
        }
    }
}
</style>
